//十滴水游戏
#include <windows.h>
#include <stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>	
#define height 800 
#define width 1300
#define time_distance 12//水滴每运动一格的帧率，仅供参考
#define TIME 10//游戏停顿时间
#define MAX_LIST 42//最大表格长宽，含缓冲，实际(MAX_LIST-1)
clock_t start=0, now=0;//前两个变量为计时器，便于控制程序运行时间。
int water_all_color = 1;//切换颜色
int rgb[10][3] = { { 0,0,0 },{50,165,50},{207,23,13},{137,36,216},{30,30,142},
	{0,0,0}, { 60,191,57 } ,{232,25,14},{153,41,242},{35,36,168} };//颜色数组前五个为填充后五个为边框
int rgb_average[5][3]{ { 0, 0, 0 },{ 55, 178, 54 },{ 220, 24, 14 },{ 145, 39, 229 },{ 33, 33, 155 } };//颜色平均值
int rgb_now =1;//当前颜色
int game_random[5] = { 18,15,12,10,0 };//以一百为标准，里面是每个水滴数量的比例，1-4
int row = 22, column =22;//行列
int max = 5;//水滴数量阈值
float distance = 0, distance_half = 0;//表格距离
int x_0 = 300, y_0 = 100;//初始位置
int x_1 = 900, y_1 = 700;
int x_mouse=0, y_mouse=0, n_mouse = 0,time_mouse = 0;//鼠标的坐标，是否获取，缓冲时间
int num_water_use = 0, num_water_boom = 0, num_water_begin = 10, currentValue = 0,num_water_all=0;//水滴点击数，炸裂数，初始数，可用数,水滴总数
int num_water_I_begin = 0;//自定义水滴初始数
int scene = 5;//打开的界面的编号
int game_scene = 3;//游戏的结局返回的编号，0代表正在游戏中，1代表再来一次，2代表退出上级界面,3为非游戏内界面,4代表结局界面
//个位数代表模式选择0为初始，1为经典，2为狂暴，3为核弹，4为塔防，5为自定义，6为性能测试，二位数代表难度，11，12,13分别代表简单，普通，困难，依次类推
int water_over = 0,ice_over = 0;//判断场上是否有水滴移动，是否有水滴
int j = 0;//没什么用的参数，主要判断函数是否退出
int mouse_late = 0;//鼠标点击后结局判断延迟的标记
int player_time = 1;//狂暴模式开启器
float play_time = 12;//游戏开始时间，毫秒
float math_circle_size[1000] = { 0.00,0.00,0.01,0.01,0.02,0.02,0.03,0.03,0.04,0.05,
0.05,0.06,0.06,0.07,0.07,0.08,0.09,0.09,0.10,0.11,0.12,0.12,0.13,
0.14,0.15,0.16,0.17,0.18,0.19,0.20,0.21,0.22,0.23,0.24,0.25,0.27,
0.28,0.29,0.30,0.32,0.33,0.34,0.35,0.37,0.38,0.40,0.41,0.42,0.44,
0.45,0.47,0.48,0.49,0.51,0.52,0.54,0.55,0.57,0.58,0.59,0.61,0.62,
0.64,0.65,0.66,0.68,0.69,0.70,0.71,0.73,0.74,0.75,0.76,0.77,
0.78,0.79,0.80,0.81,0.82,0.83,0.84,0.85,0.86,0.87,0.88,0.89,0.89,0.90,0.91,
0.91,0.92,0.93,0.93,0.94,0.95,0.95,0.96,0.96,0.97,0.97,0.98,0.98,0.99,0.99,1.00 };//物理拟合圆的弧度与面积的关系
float math_circle_hudu[1000] = { 0.00,0.03,0.06,0.09,0.12,0.15,0.18,0.21,0.24,0.27,0.30,0.33,
0.36,0.39,0.42,0.45,0.48,0.51,0.54,0.57,0.60,0.63,0.66,0.69,0.72,0.75,0.78,
0.81,0.84,0.87,0.90,0.93,0.96,0.99,1.02,1.05,1.08,1.11,1.14,1.17,1.20,1.23,
1.26,1.29,1.32,1.35,1.38,1.41,1.44,1.47,1.50,1.53,1.56,1.59,1.62,1.65,1.68,
1.71,1.74,1.77,1.80,1.83,1.86,1.89,1.92,1.95,1.98,2.01,2.04,2.07,2.10,2.13,
2.16,2.19,2.22,2.25,2.28,2.31,2.34,2.37,2.40,2.43,2.46,2.49,2.52,2.55,2.58,
2.61,2.64,2.67,2.70,2.73,2.76,2.79,2.82,2.85,2.88,2.91,2.94,2.97,3.00,3.03,3.06,3.09,3.15, };
struct condition//每个格子的状态
{
	int num;//有几个固定水
	int up;//有几个喷射水,[5]表示5种颜色
	int down;
	int left;
	int right;
};
condition game[MAX_LIST][MAX_LIST];//仅存储1-20
condition game_temp[MAX_LIST][MAX_LIST];//临时存储
void clock_sleep();//控制程序运行精确时间
int randombetween(int a, int b);//随机数
void game_begin();//初始化全都为0和行列
void input_game();//输入数据
void game_fuzhi(struct condition a[MAX_LIST][MAX_LIST], struct condition b[MAX_LIST][MAX_LIST]);//将结构体a复制到结构体b
void condition_water_num();//更新固定水滴数量
void condition_update();//更新运动水滴状态
void draw_ice(float x, float y, int row, int column, int n_water);//绘制固定水滴
void draw_water(float x, float y, int row_0, int column_0, int j);//绘制运动水滴
void draw_lines(); // 绘制网格线
void mouse();//检测鼠标左键坐标
void mouse_count();//根据鼠标坐标计算各种坐标
void mouse_water();//根据鼠标坐标增加水滴
void mouse_result_100();//自定义结局
void continue_result();//重新开始的结局处理
void draw_scene_5();//第一个界面的绘制
void scene_5_mouse();//第一个界面的鼠标检测
void begin();//游戏内初始化总函数
void count(int p);//游戏内计算总函数
void play();//游玩过程的通用函数
void draw(int p);//游玩内绘制总函数
void bug();//超出边界警告
void draw_circle();//绘制显示水滴数量的函数
void circle_time();//绘制计时圆
void game_over();//游戏结束
int main()
{
	initgraph(width, height);
	while (1)
	{
		//第一级界面模式选择
		//第二极界面难度调整
		mouse();
		mouse_count();
		draw_scene_5();
		if (game_scene == 0)
			play();//第三极界面开始游戏
	}
	_getch();
	return 0;
}
void clock_sleep()//控制程序运行精确时间
{
	static clock_t start = clock(); // 静态变量保持时间戳
	clock_t now = clock();

	// 计算经过的时间（毫秒）
	int elapsed = static_cast<int>(1000.0 * (now - start) / CLOCKS_PER_SEC);
	// 计算剩余时间
	int remaining = TIME - elapsed;

	// 如果剩余时间大于0，才进行休眠
	if (player_time == -1 && remaining > 0) {
		// 使用 busy-waiting 实现更精确的延时
		clock_t end_time = now + static_cast<clock_t>(remaining * CLOCKS_PER_SEC / 1000);
		while (clock() < end_time) {
			// 空循环，等待时间到达
		}
		play_time -= static_cast<float>(TIME) / 1000;
	}
	else
		play_time -= static_cast<float>(elapsed) / 1000;
	// 更新开始时间为当前时间
	start = clock();
}
int randombetween(int a, int b)//随机数
{
	int r = rand() % (b - a + 1) + a;
	return r;
}
void game_begin()//初始化全都为0和行列
{
	for (int i = 0; i <= (MAX_LIST-1); i++)
	{
		for (int j = 0; j <= (MAX_LIST-1); j++)
		{
			game[i][j].num = 0;
			game[i][j].up = 0;
			game[i][j].down = 0;
			game[i][j].left = 0;
			game[i][j].right = 0;
		}
	}
	num_water_use = 0;
	num_water_boom = 0;
	num_water_all = row*column;
	num_water_begin = num_water_I_begin;
	ice_over = 1;
	water_over = 1;
	int p = row;
	if (column > row)
		p = column;
	distance = (float)(x_1 - x_0) / p;
	distance_half = distance/2;
	if (player_time == 1)
	
		play_time = 12;
	
	else if (player_time == -1)
		 play_time = 10;

}
void input_game()//输入数据
{
	for (int i = 1; i <= row; i++)
	{
		for (int j = 1; j <= column; j++)
		{
			int p = randombetween(1, 100);
			if (p <= game_random[0])
				game[i][j].num = 1;
			else if (p <= game_random[0] + game_random[1])
				game[i][j].num = 2;
			else if (p <= game_random[0] + game_random[1] + game_random[2])
				game[i][j].num = 3;
			else if (p <= game_random[0] + game_random[1] + game_random[2] + game_random[3])
				game[i][j].num = 4;
			else if (p <= game_random[0] + game_random[1] + game_random[2] + game_random[3] + game_random[4])
				game[i][j].num = 5;
			else
				num_water_all -= 1;
		}
	}

}
void game_fuzhi(struct condition a[MAX_LIST][MAX_LIST], struct condition b[MAX_LIST][MAX_LIST])//将结构体a复制到结构体b
{
	for (int i = 1; i <= row; i++)
	{
		for (int j = 1; j <= column; j++)
		{
			b[i][j].num = a[i][j].num;
			b[i][j].up = a[i][j].up;
			b[i][j].down = a[i][j].down;
			b[i][j].left= a[i][j].left;
			b[i][j].right= a[i][j].right;

		}
	}
}
void condition_water_num()//更新固定水滴数量
{
		for (int i = 1; i <= row; i++)
		{
			for (int j = 1; j <= column; j++)
			{
				if (game[i][j].num >= 1)
				{
					if (game[i][j].num >= max)
					{
						int t = 1;
						if (game[i][j].num >= 8)
							t = 2;
						game[i][j].num = 0;
						game[i][j].up += t;
						game[i][j].down += t;
						game[i][j].left+= t;
						game[i][j].right += t;
						num_water_boom++;
						water_over = 1;//表示有移动水
						if (player_time == -1&&(randombetween(1, 16))>>4)//奖励时间
							play_time += 1;
						if (num_water_boom == 1)//计时开始
						{
							if (player_time == 1)
							{
								play_time = 12;
							}
							else if (player_time == -1) {
								play_time = 10;
							}
						}
					}
				}
			}
		}
		currentValue = -num_water_use + num_water_begin + num_water_boom;
}
void condition_update()//更新运动水滴状态
{
		water_over = 0;
		game_fuzhi(game, game_temp);//复制game到game_temp
		for (int i = 1; i <= row; i++)
		{
			for (int j = 1; j <= column; j++)
			{
					if (game[i][j].up >= 1)
						//水向上，若上方有且颜色相同，则合并，否则向上喷射
					{
						water_over = 1;
						int num = game[i][j].up;//水滴数量
						game_temp[i][j].up = 0;//水滴射出，当前位置清空
						if (game_temp[i - 1][j].num >= 1)
						{
							game_temp[i - 1][j].num += num;
							if(game[i - 1][j].num+num>=8)
							game_temp[i - 1][j].up += game[i - 1][j].num+num-6;
						}
						else
							game_temp[i - 1][j].up += num;
					}
					if (game[i][j].down >= 1)
						//水向下，若下方有且颜色相同，则合并，否则向下喷射
					{
						water_over = 1;
						int num = game[i][j].down;//水滴数量
						game_temp[i][j].down = 0;//水滴射出，当前位置清空
						if (game_temp[i + 1][j].num >= 1)
						{
							game_temp[i + 1][j].num += num;
							if(game[i + 1][j].num+num>=8)
							game_temp[i + 1][j].down += game[i + 1][j].num+num-6;
						}
						else
							game_temp[i + 1][j].down += num;
					}
					if (game[i][j].left >= 1)
						//水向左，若左方有且颜色相同，则合并，否则向左喷射
					{
						water_over = 1;
						int num = game[i][j].left;//水滴数量
						game_temp[i][j].left = 0;//水滴射出，当前位置清空
						if (game_temp[i][j - 1].num >= 1)
						{
							game_temp[i][j-1].num += num;
							if (game[i][j-1].num+num >= 8)
								game_temp[i][j-1].left += game[i][j-1].num+num - 6;
						}
						else
							game_temp[i][j - 1].left += num;
					}
					if (game[i][j].right >= 1)
						//水向右，若右方有且颜色相同，则合并，否则向右喷射
					{
						water_over = 1;
						int num = game[i][j].right;//水滴数量
						game_temp[i][j].right = 0;//水滴射出，当前位置清空
						if (game_temp[i][j + 1].num >= 1)
						{
							game_temp[i][j+1].num += num;
							if (game[i][j+1].num+num >= 8)
								game_temp[i][j+1].right += game[i][j+1].num+num - 6;
						}
						else
							game_temp[i][j + 1].right += num;
					}
			}
		}
		game_fuzhi(game_temp, game);
}
void draw_ice(float x, float y, int row, int column, int n_water)//绘制固定水滴
{
	if (n_water != 0)
	{
		// 设置填充颜色和线条颜色
		setfillcolor(RGB(rgb[rgb_now + 5][0], rgb[rgb_now + 5][1], rgb[rgb_now + 5][2]));
		setlinecolor(RGB(rgb[rgb_now + 5][0], rgb[rgb_now + 5][1], rgb[rgb_now + 5][2]));
		// 根据 n_water 的值绘制不同的圆外部
		if (n_water == 1) {
			// 绘制第一种圆，若干个
			fillcircle(x + 0.1 * distance_half, y + 0.1 * distance_half, 0.3 * distance_half );
			fillcircle(x - 0.6 * distance_half, y - 0.2 * distance_half, 0.11 * distance_half);
			fillcircle(x - 0.3 * distance_half, y + 0.6 * distance_half, 0.13 * distance_half);
		}
		else if (n_water == 2) {
			// 绘制第二种圆，若干个
			fillcircle(x, y, 0.45 * distance_half); // 大圆
			fillcircle(x + 0.5 * distance_half, y + 0.5 * distance_half, 0.23 * distance_half); // 小圆1
		}
		else if (n_water == 3) {
			// 绘制第三种圆，若干个
			fillcircle(x + 0.35 * distance_half, y + 0.3 * distance_half, 0.63 * distance_half);
			fillcircle(x - 0.35 * distance_half, y + 0.1 * distance_half, 0.35 * distance_half);
		}
		else if (n_water == 4) {
			// 绘制第四种圆，若干个
			fillcircle(x, y, 0.79 * distance_half);
		}
		setfillcolor(RGB(rgb[rgb_now][0], rgb[rgb_now][1], rgb[rgb_now][2]));
		// 绘制内部
		if (n_water == 1) {
			// 绘制第一种圆，若干个
			fillcircle(x + 0.1 * distance_half, y + 0.1 * distance_half, 0.25 * distance_half);
			fillcircle(x - 0.6 * distance_half, y - 0.2 * distance_half, 0.08 * distance_half);
			fillcircle(x - 0.3 * distance_half, y + 0.6 * distance_half, 0.1 * distance_half);
		}
		else if (n_water == 2) {
			// 绘制第二种圆，若干个
			fillcircle(x, y, 0.4 * distance_half); // 大圆
			fillcircle(x + 0.5 * distance_half, y + 0.5 * distance_half, 0.2 * distance_half); // 小圆1
		}
		else if (n_water == 3) {
			// 绘制第三种圆，若干个
			fillcircle(x + 0.35 * distance_half, y + 0.3 * distance_half, 0.55 * distance_half);
			fillcircle(x - 0.35 * distance_half, y + 0.1 * distance_half, 0.3 * distance_half);
		}
		else if (n_water == 4) {
			// 绘制第四种圆，若干个
			fillcircle(x, y, 0.7 * distance_half);
		}
	}
}
void draw_water(float x, float y, int row_0, int column_0, int j)//绘制运动水滴
{
	//先统一用绿色
	//将距离平均分为20份，n_water代表两中心点距离份数
	//需要分别对上下左右四个方向进行发射
	//通过行列数组得到四个方向水滴的个数，不为0则绘制
	float y1 = 0, y2 = 0, x1 = 0, x2 = 0;
	int k = rgb_now;
		setfillcolor(RGB(rgb_average[k][0], rgb_average[k][1], rgb_average[k][2]));
		setlinecolor(RGB(rgb_average[k][0], rgb_average[k][1], rgb_average[k][2]));
		float radius;
		float water_time_distance = (j - 5) * distance / 30;
		if (game[row_0][column_0].up != 0)
		{
			radius = (2 * game[row_0][column_0].up + 2) * distance * 0.01;
			if (row_0 > 1)
			{
				y1 = y - distance / 12 * j;
				fillcircle(x, y1, radius + 0.05 * distance_half);
			}
			else if (row_0 == 1 && j <= 5)
			{
				y1 = y - distance / 12 * j;
				fillcircle(x, y1, radius + 0.05 * distance_half);
			}
			else if (row_0 == 1 && j > 5&&row<=16&&column<=16)
			{
				y1 = y - distance_half;
				radius = radius / 3 + 0.02 * distance_half;
				fillcircle(x, y1 + radius + water_time_distance, radius);
				fillcircle(x + water_time_distance, y1 + radius, radius);
				fillcircle(x - water_time_distance, y1 + radius, radius);
				fillcircle(x + water_time_distance * 0.7, y1 + radius + water_time_distance * 0.7, radius);
				fillcircle(x - water_time_distance * 0.7, y1 + radius + water_time_distance * 0.7, radius);
			}
		}
		if (game[row_0][column_0].down != 0)
		{
			radius = (2 * game[row_0][column_0].down + 2) * distance * 0.01;
			if (row_0 < row)
			{
				y2 = y + distance / 12 * j;
				fillcircle(x, y2, radius + 0.05 * distance_half);
			}
			else if (row_0 == row && j <= 5)
			{
				y2 = y + distance / 12 * j;
				fillcircle(x, y2, radius + 0.05 * distance_half);
			}
			else if (row_0 == row && j > 5 && row <= 16 && column <= 16)
			{
				y2 = y + distance_half;
				radius = radius / 3 + 0.02 * distance_half;
				fillcircle(x, y2 - radius - water_time_distance, radius);
				fillcircle(x + water_time_distance, y2 - radius, radius);
				fillcircle(x - water_time_distance, y2 - radius, radius);
				fillcircle(x + water_time_distance * 0.7, y2 - radius - water_time_distance * 0.7, radius);
				fillcircle(x - water_time_distance * 0.7, y2 - radius - water_time_distance * 0.7, radius);
			}
		}
		if (game[row_0][column_0].left!= 0)
		{
			radius = (2 * game[row_0][column_0].left + 2) * distance * 0.01;
			if (column_0 > 1)
			{
				x1 = x - distance / 12 * j;
				fillcircle(x1, y, radius + 0.05 * distance_half);
			}
			else if (column_0 == 1 && j <= 5)
			{
				x1 = x - distance / 12 * j;
				fillcircle(x1, y, radius + 0.05 * distance_half);
			}
			else if (column_0 == 1 && j > 5 && row <= 16 && column <= 16)
			{
				x1 = x - distance_half;
				radius = radius / 3 + 0.02 * distance_half;
				fillcircle(x1 + radius + water_time_distance, y, radius);
				fillcircle(x1 + radius, y + water_time_distance, radius);
				fillcircle(x1 + radius, y - water_time_distance, radius);
				fillcircle(x1 + radius + water_time_distance * 0.7, y + water_time_distance * 0.7, radius);
				fillcircle(x1 + radius + water_time_distance * 0.7, y - water_time_distance * 0.7, radius);
			}
		}
		if (game[row_0][column_0].right!= 0)
		{
			radius = (2 * game[row_0][column_0].right + 2) * distance * 0.01;
			if (column_0 < column)
			{
				x2 = x + distance / 12 * j;
				fillcircle(x2, y, radius + 0.05 * distance_half);
			}
			else if (column_0 == column && j <= 5)
			{
				x2 = x + distance / 12 * j;
				fillcircle(x2, y, radius + 0.05 * distance_half);
			}
			else if (column_0 == column && j > 5 && row <= 16 && column <= 16)
			{
				x2 = x + distance_half;
				radius = radius / 3 + 0.02 * distance_half;
				fillcircle(x2 - radius - water_time_distance, y, radius);
				fillcircle(x2 - radius, y + water_time_distance, radius);
				fillcircle(x2 - radius, y - water_time_distance, radius);
				fillcircle(x2 - radius - water_time_distance * 0.7, y + water_time_distance * 0.7, radius);
				fillcircle(x2 - radius - water_time_distance * 0.7, y - water_time_distance * 0.7, radius);
			}
	}
}
void draw_lines() // 绘制网格线
{
	// 设置线条颜色为天蓝色
	setfillcolor(RGB(135, 206, 250)); // 使用RGB宏设置线条颜色为天蓝色
	setlinecolor(RGB(135, 206, 250)); // 使用RGB宏设置线条颜色为天蓝色
	// 绘制横线
	for (int k = 0; k <= row; k++) { // 修改为 k 从 0 到 row (包括底边线)
		int y =(int) (distance * k + y_0); // 每条线的y坐标是正确的框的边缘
		line(x_0, y, x_0 + column * distance , y); // 绘制每一行横线
	}

	// 绘制竖线
	for (int l = 0; l <= column; l++) { // 修改为 l 从 0 到 column (包括右边线)
		int x = (int)(distance * l + x_0); // 每条线的x坐标是正确的框的边缘
		line(x, y_0, x, y_0 + row * distance); // 绘制每一列竖线
	}
}
void mouse() //检测鼠标左键坐标
{
	time_mouse -= TIME;
	if ((GetAsyncKeyState(VK_LBUTTON) & 0x8000) && time_mouse <= 0) {
		time_mouse = 120;
		// 获取鼠标在屏幕上的位置
		POINT cursorPos;
		GetCursorPos(&cursorPos); // 获取屏幕坐标
		// 获取当前活动窗口的句柄
		HWND hwnd = GetForegroundWindow(); // 获取前景窗口句柄
		RECT windowRect;
		// 获取活动窗口的位置
		if (GetWindowRect(hwnd, &windowRect)) {
			// 计算相对窗口左上角的坐标
			x_mouse = cursorPos.x - windowRect.left; // 获取相对 x 坐标
			y_mouse = cursorPos.y - windowRect.top;  // 获取相对 y 坐标
			// 获取标题栏和边框的高度
			int titleBarHeight = GetSystemMetrics(SM_CYSIZEFRAME) + GetSystemMetrics(SM_CYCAPTION);
			// 减去标题栏和边框的高度
			y_mouse -= titleBarHeight; // 调整 y 坐标
			// 检查坐标是否在窗口客户区内
			if (x_mouse < 0 || y_mouse < 0 ||
				x_mouse >= windowRect.right - windowRect.left ||
				y_mouse >= windowRect.bottom - windowRect.top - titleBarHeight) {
				// 鼠标在窗口外，不进行任何处理
				n_mouse = 0; // 可以选择重置鼠标标记
			}
			else {
				n_mouse = 1; // 标记鼠标位置已获取
			}
		}
	}
}
void mouse_count()//根据鼠标坐标计算各种坐标
{
	if (n_mouse == 1)
	{
		n_mouse = 0;
		
		if (game_scene != 3)
		{
			if (game_scene == 0)//自定义界面游玩为100
			{
				mouse_water();
			}
			else if (game_scene == 4)//自定义模式界面结局游戏结算界面1
			{
				mouse_result_100();
			}
		}
		else if(game_scene==3)
		{
			if (scene == 0)//自定义模式界面编号为0,初始界面
			{
				//
			}
			else if (scene == 1)//自定义模式界面编号为5,自定义模式界面数值调整
			{
				//
			}
			else if (scene == 2)//自定义模式界面编号为5,自定义模式界面数值调整
			{
				//
			}
			else if (scene == 3)//自定义模式界面编号为5,自定义模式界面数值调整
			{
				//
			}
			else if (scene == 4)//自定义模式界面编号为5,自定义模式界面数值调整
			{
				//
			}
			else if (scene == 5)//自定义模式界面编号为5,自定义模式界面数值调整
			{
				scene_5_mouse();
			}
		}
	}
}
void mouse_water()//根据鼠标坐标增加水滴
{
	if (currentValue!= 0)
	{
		for (int k = 1; k <= row; k++)//y
		{
			for (int l = 1; l <= column; l++)//x
			{
				float x = distance * (l - 0.5) + x_0;//中心坐标
				float y = distance * (k - 0.5) + y_0;
				if (game[k][l].num >= 1&& game[k][l].num <= 4) {
					if (x_mouse<x+7 + distance/2 && x_mouse>x+7 - distance/2&& y_mouse<y + distance_half && y_mouse>y - distance_half)
					{
							game[k][l].num += 1;
							num_water_use++;
							currentValue--;//不会影响现在水滴，目的防止水滴为负数
							mouse_late = 1;//防止异常结局
							if (player_time == 1)
							{
								game[k][l].num += 5;
								game[k][l].left+= 4;
								game[k][l].right+= 4;
								game[k][l].up +=4;
								game[k][l].down+= 4;
								play_time -= 0.5;
							}
					}
				}
			}
		}
	}
}
void mouse_result_100()//自定义结局
{
	if (x_mouse >= 410 && x_mouse <= 800 && y_mouse >= 440 && y_mouse <= 530)
	{
		j = 1;
		game_scene = 1;//重来
	}
	else if (x_mouse >= 1100 && y_mouse <= 75)
	{
		j = 1;
		game_scene =2;//退出
	}
}
void draw_scene_5()//第一个界面的绘制
{
	setbkmode(1);
	int h_1 = 100;
	int h_2 = 260;
	int h_3 = 420;
	int h_4 = 600;//矩形上边缘
	int h_5 = 210;
	int w_1 = 200;
	int w_2 = 800;
	int len = 400;//矩形长
	BeginBatchDraw();
	cleardevice();
	setbkcolor(BLACK);
	setfillcolor(WHITE);
	setlinecolor(RGB(128,128,128));
	fillrectangle(w_1-10, h_4, w_1-10 + len, h_4 + 100);//返回的矩形
	fillrectangle(w_2-10, h_4, w_2-10 + len, h_4 + 100);//确定的矩形
	fillrectangle(w_1-10, h_1, w_1-10 + len, h_1 + 80);//行数的矩形
	fillrectangle(w_1-10, h_2, w_1-10 + len, h_2 + 80);//列数的矩形
	fillrectangle(w_1-10, h_3, w_1-10 + len, h_3 + 80);//初始水滴数的矩形
	fillrectangle(w_2-10, h_1, w_2-10 + len, h_1 + 80);//密度的矩形
	fillrectangle(w_2-10, h_5-10, w_2-10 + len, h_5 + 300);//密度参数的矩形
	fillrectangle(0, 0, 240, 50);
	settextcolor(BLACK);
	TCHAR s[40]; // 增加字符数组大小
	swprintf_s(s, _T("返回")); // 
	settextstyle(100, 0, _T("宋体")); // 设置文本样式
	outtextxy(w_1 + 100, h_4, s); // 起点
	swprintf_s(s, _T("确认")); // 
	settextstyle(100, 0, _T("宋体")); // 设置文本样式
	outtextxy(w_2 + 100, h_4, s); //
	swprintf_s(s, _T("行数：%2d + -"), row);//行数
	settextstyle(40, 0, _T("宋体")); // 设置文本样式
	outtextxy(w_1, h_1 + 20, s);
	swprintf_s(s, _T("列数：%2d + -"), column);//列数
	settextstyle(40, 0, _T("宋体")); // 设置文本样式
	outtextxy(w_1, h_2 + 20, s);
	swprintf_s(s, _T("初始水%2d + -"), num_water_begin);//初始水滴数
	settextstyle(40, 0, _T("宋体")); // 设置文本样式
	outtextxy(w_1, h_3 + 20, s);
	swprintf_s(s, _T("密度"));//密度
	settextstyle(40, 0, _T("宋体")); // 设置文本样式
	outtextxy(w_2 + 160, h_1 + 20, s);
	swprintf_s(s, _T("一滴概率：%.2f + -"), (float)game_random[0] / 100);//一滴概率
	settextstyle(40, 0, _T("宋体")); // 设置文本样式
	outtextxy(w_2, h_5, s);
	swprintf_s(s, _T("两滴概率：%.2f + -"), (float)game_random[1] / 100);//两滴概率
	settextstyle(40, 0, _T("宋体")); // 设置文本样式
	outtextxy(w_2, h_5 + 80, s);
	swprintf_s(s, _T("三滴概率：%.2f + -"), (float)game_random[2] / 100);//三滴概率
	settextstyle(40, 0, _T("宋体")); // 设置文本样式
	outtextxy(w_2, h_5 + 160, s);
	swprintf_s(s, _T("四滴概率：%.2f + -"), (float)game_random[3] / 100);//四滴概率
	settextstyle(40, 0, _T("宋体")); // 设置文本样式
	outtextxy(w_2, h_5 + 240, s);
	swprintf_s(s, _T("狂暴模式")); // 狂暴模式
	settextstyle(40, 0, _T("宋体")); // 设置文本样式
	outtextxy(0, 5, s);
	if (player_time == 1)
	swprintf_s(s, _T("√")); // 狂暴模式
	else
	swprintf_s(s, _T("□")); // 狂暴模式
	settextstyle(40, 0, _T("宋体")); // 设置文本样式
	outtextxy(200, 5, s);
	if(player_time==1)
		settextcolor(RED);
	swprintf_s(s, _T("狂暴模式")); // 狂暴模式
	settextstyle(40, 0, _T("宋体")); // 设置文本样式
	outtextxy(0, 5, s);
	EndBatchDraw();
	Sleep(TIME);
}
void continue_result()//重新开始的结局处理
{
	j = 0;
	if (player_time == 1)
		time_mouse = 1000;
	else
		time_mouse = 500;
}
void scene_5_mouse()//第一个界面的鼠标检测
{
	/*坐标点
	390,140;430,140
	390,300;430,300
	510,460;550,460
	1110,230;1150,230
	1110,310;1150,310
	1110,390;1150,390
	1110,470;1150,470
	边长为20
	*/
	int r = 10;
	if (x_mouse >= 396 - r && x_mouse <= 396 + r && y_mouse >= 140 - r && y_mouse <= 140 + r)//行+
	{
		if (row < (MAX_LIST - 2))
			row += 1;
		else
			bug();
	}
	else if (x_mouse >= 436 - r && x_mouse <= 436 + r && y_mouse >= 140 - r && y_mouse <= 140 + r)//行-
	{
		if (row > 1)
			row -= 1;
		else
			bug();
	}
	else if (x_mouse >= 396 - r && x_mouse <= 396 + r && y_mouse >= 300 - r && y_mouse <= 300 + r)//列+
	{
		if (column < (MAX_LIST - 2))
			column += 1;
		else
			bug();
	}
	else if (x_mouse >= 436 - r && x_mouse <= 436 + r && y_mouse >= 300 - r && y_mouse <= 300 + r)//列-
	{
		if (column > 1)
			column -= 1;
		else
			bug();
	}
	else if (x_mouse >= 396 - r && x_mouse <= 396 + r && y_mouse >= 460 - r && y_mouse <= 460 + r)//水滴初始+
	{
		num_water_begin += 1;
	}
	else if (x_mouse >= 436 - r && x_mouse <= 436 + r && y_mouse >= 460 - r && y_mouse <= 460 + r)//水滴初始-
	{
		if (num_water_begin > 1)
			num_water_begin -= 1;
		else
			bug();
	}
	else if (x_mouse >= 1116 - r && x_mouse <= 1116 + r && y_mouse >= 230 - r && y_mouse <= 230 + r)//密度1+
	{
		if (game_random[0] < 100 && game_random[0] + game_random[1] + game_random[2] + game_random[3] < 100)
			game_random[0] += 1;
		else
			bug();
	}
	else if (x_mouse >= 1156 - r && x_mouse <= 1156 + r && y_mouse >= 230 - r && y_mouse <= 230 + r)//密度1-
	{
		if (game_random[0] > 0)
			game_random[0] -= 1;
		else
			bug();
	}
	else if (x_mouse >= 1116 - r && x_mouse <= 1116 + r && y_mouse >= 310 - r && y_mouse <= 310 + r)//密度2+
	{
		if (game_random[1] < 100 && game_random[0] + game_random[1] + game_random[2] + game_random[3] < 100)
			game_random[1] += 1;
		else
			bug();
	}
	else if (x_mouse >= 1156 - r && x_mouse <= 1156 + r && y_mouse >= 310 - r && y_mouse <= 310 + r)//密度2-
	{
		if (game_random[1] > 0)
			game_random[1] -= 1;
		else
			bug();
	}
	else if (x_mouse >= 1116 - r && x_mouse <= 1116 + r && y_mouse >= 390 - r && y_mouse <= 390 + r)//密度3+
	{
		if (game_random[2] < 100 && game_random[0] + game_random[1] + game_random[2] + game_random[3] < 100)
			game_random[2] += 1;
		else
			bug();
	}
	else if (x_mouse >= 1156 - r && x_mouse <= 1156 + r && y_mouse >= 390 - r && y_mouse <= 390 + r)//密度3-
	{
		if (game_random[2] > 0)
			game_random[2] -= 1;
		else
			bug();
	}
	else if (x_mouse >= 1116 - r && x_mouse <= 1116 + r && y_mouse >= 470 - r && y_mouse <= 470 + r)//密度4+
	{
		if (game_random[3] < 100 && game_random[0] + game_random[1] + game_random[2] + game_random[3] < 100)
			game_random[3] += 1;
		else
			bug();
	}
	else if (x_mouse >= 1156 - r && x_mouse <= 1156 + r && y_mouse >= 470 - r && y_mouse <= 470 + r)//密度4-
	{
		if (game_random[3] > 0)
			game_random[3] -= 1;
		else
			bug();
	}
	else if (x_mouse >= 206 && x_mouse <= 606 && y_mouse >= 600 && y_mouse <= 700)
	{
		//scene = 0;
		//重置游戏界面
	}
	else if (x_mouse >= 806 && x_mouse <= 1206 && y_mouse >= 600 && y_mouse <= 700)
	{
		game_scene = 0;
		//开始游戏
	}
	else if(x_mouse>=200&&x_mouse<=240&&y_mouse>=0&&y_mouse<=50)
	player_time=-player_time;
	num_water_I_begin = num_water_begin;//水滴点击数
}
void begin()//游戏内初始化总函数
{
	game_scene = 0;
	srand(time(0));//随机数种子
	game_begin();
	input_game();
	cleardevice();
	Sleep(10);
}
void count(int p)//游戏内计算总函数
{
	mouse();
	mouse_count();
	if (p == 0)
	{
		condition_update();
		condition_water_num();
		mouse_late = 0;
	}
	else if (p == 10&&mouse_late ==0)
		game_over();//结局判断
	clock_sleep();//精确延时
}
void play()//游玩过程的通用函数
{
	for (j = 0; j == 0;) 
	{
		begin();
		for (int p = 0; j == 0; p++)
		{
			count(p% 12);//鼠标与计算
			draw(p % 12);//绘制
		}
		if (game_scene == 1)//再来一次
		{
			continue_result();//重新开始的结局处理
			continue;
		}
		game_scene = 3;//运行结束后返回界面1即上级界面
	}
}
void draw(int p)//游玩内绘制总函数
{
	if (player_time != 1 || p % 2 == 0 ) {
		BeginBatchDraw();
		setbkcolor(BLACK);
		cleardevice();
		draw_lines();//画网格线
		for (int k = 1; k <= row; k++)//行
		{
			for (int l = 1; l <= column; l++)//列
			{
				float x = distance * (l - 0.5) + x_0;
				float y = distance * (k - 0.5) + y_0;
				draw_ice(x, y, k, l,game[k][l].num);//画固定水
				draw_water(x, y, k, l, p);//画移动水
			}
		}
		
		draw_circle();//画水滴数量圆
		circle_time();//画计时圆
		EndBatchDraw();
	}
}
void bug()//超出边界警告
{
	BeginBatchDraw();
	printf("\a");
	setcolor(RED);
	TCHAR s[40]; // 增加字符数组大小
	swprintf_s(s, _T("超出边界！")); // 
	settextstyle(200, 0, _T("宋体")); // 设置文本样式
	outtextxy(width / 2 - 400, height / 2 - 100, s); // 
	setcolor(BLACK);
	EndBatchDraw();
	Sleep(1000);
}
void draw_circle()//绘制显示水滴数量的函数
{
	TCHAR s[40];//狂暴模式
	if (player_time == 1)
	{
		settextcolor(RED);
		swprintf_s(s, _T("狂暴模式")); // 狂暴模式
		settextstyle(40, 0, _T("宋体")); // 设置文本样式
		outtextxy(0, 5, s);
	}
	//
	int cirx = 1100, ciry = 400; // 圆心坐标
	int r = 160; // 圆的半径
	setlinecolor(RGB(55, 178, 54));
	setfillcolor(RGB(55, 178, 54));
	circle(cirx, ciry, r);
	TCHAR v[40]; // 存储展示水滴文字
	if (player_time == -1) {
		if (currentValue <= 59 && currentValue >= 1)
		{
			float hudu_fill=0;
			float percent = (float)currentValue / 30;
			if (currentValue <= 30) {
				int i = 0;
				for (; i < 105; i++)
				{
					if (math_circle_size[i] <= percent && math_circle_size[i + 1] >= percent)
					{
						hudu_fill=math_circle_hudu[i];
						break;
					}
				}
				float height_h = r * cos(hudu_fill);
				float height_x = fabs(r * sin(hudu_fill));
				line(cirx - height_x, ciry + height_h, cirx + height_x, ciry + height_h); // 画水的高度线
				floodfill(cirx, ciry + r - 1, RGB(55, 178, 54));
			}
			else
			{
				percent-=1;
				int i = 0;
				for (; i < 105; i++)
				{
					if (math_circle_size[i] <= percent && math_circle_size[i + 1] >= percent)
					{
						hudu_fill = math_circle_hudu[i];
						break;
					}
				}
				float height_h = r * cos(hudu_fill);
				float height_x = fabs(r * sin(hudu_fill));
				fillcircle(cirx, ciry, r);
				setlinecolor(GREEN);
				setfillcolor(GREEN);
				circle(cirx, ciry, r);
				line(cirx - height_x, ciry + height_h, cirx + height_x, ciry + height_h); // 画水的高度线
				floodfill(cirx, ciry + r - 1, GREEN);
			}
		}
		else if (currentValue >= 60)
		{
			setlinecolor(GREEN);
			setfillcolor(GREEN);
			fillcircle(cirx, ciry, r);
		}
		settextcolor(RGB(152, 251, 152));
		settextstyle(100, 0, _T("宋体")); // 设置文本样式
		swprintf_s(v, _T("%d"), currentValue); // 格式化水滴数值字符串
		int textWidth = textwidth(v);//计算文本的宽度
		int textHeight = textheight(v);
		outtextxy(cirx - textWidth / 2, ciry - textHeight / 2, v); // 显示水滴数量
	}
	else if (player_time == 1)
	{
		float hudu_fill=0;
		float percent = (float)num_water_boom / num_water_all*2;
		if (percent <= 1&&percent>0) {
			int i = 0;
			for (; i < 105; i++)
			{
				if (math_circle_size[i] <= percent && math_circle_size[i + 1] >= percent)
				{
					hudu_fill = math_circle_hudu[i];
					break;
				}
			}
				float height_h = r * cos(hudu_fill);
				float height_x = fabs(r * sin(hudu_fill));
				line(cirx - height_x, ciry + height_h, cirx + height_x, ciry + height_h); // 画水的高度线
				floodfill(cirx, ciry + r - 1, RGB(55, 178, 54));
		}
		else if (percent >= 1)
		{
				percent -= 1;
				int i = 0;
				for (; i < 105; i++)
				{
					if (math_circle_size[i] <= percent && math_circle_size[i + 1] >= percent)
					{
						hudu_fill = math_circle_hudu[i];
						break;
					}
				}
				float height_h = r * cos(hudu_fill);
				float height_x = fabs(r * sin(hudu_fill));
				fillcircle(cirx, ciry, r);
				setlinecolor(GREEN);
				setfillcolor(GREEN);
				circle(cirx, ciry, r);
				line(cirx - height_x, ciry + height_h, cirx + height_x, ciry + height_h); // 画水的高度线
				floodfill(cirx, ciry + r - 1, GREEN);
		}
		settextcolor(RGB(152, 251, 152));
		settextstyle(100, 0, _T("宋体")); // 设置文本样式
		swprintf_s(v, _T("%.0f%%"), (float)num_water_boom / num_water_all*100); // 格式化水滴数值字符串
		int textWidth = textwidth(v);//计算文本的宽度
		int textHeight = textheight(v);
		outtextxy(cirx - textWidth / 2, ciry - textHeight / 2, v); // 显示水滴数量
	}
	//
}
void circle_time()//绘制计时圆
{
	float t = play_time;
	int x = x_0 / 2, y = height / 2, r = 140;
	setfillcolor(RGB(181, 126, 230));
	setlinecolor(RGB(181, 126, 230));
	circle(x, y, r);
	TCHAR s[20]; // 增加字符数组大小
	settextstyle(80, 0, _T("宋体")); // 设置文本样式
	settextcolor(RGB(168, 80, 188));
	if (num_water_boom == 0)
	{
		if (player_time == -1)
			t = 10;
		else if (player_time == 1)
			t = 12;
		float hudu = (2 - t / 12 * 2) * 3.14;
		line(x, y, x, y - r);  // 绘制指向12点位置的指针
		line(x, y, x + r * sin(hudu), y - r * cos(hudu));
		floodfill(x - 1, y - r + 1, RGB(181, 126, 230));
		setlinecolor(RGB(168, 80, 188));
		swprintf_s(s, _T("%d"), (int)t);
		int textWidth = textwidth(s); // 计算文本的宽度
		int textHeight = textheight(s); // 计算文本的高度
		outtextxy(x - textWidth / 2, y - textHeight / 2 - 5, s);

	}
	else if (t >= 12)
	{
		fillcircle(x, y, r);
		swprintf_s(s, _T("%d"), (int)play_time);
		int textWidth = textwidth(s); // 计算文本的宽度
		int textHeight = textheight(s); // 计算文本的高度
		outtextxy(x - textWidth / 2, y - textHeight / 2 - 5, s);
	}
	else if(t <= 0.01)
	{
		swprintf_s(s, _T("%d"), 0);
		int textWidth = textwidth(s); // 计算文本的宽度
		int textHeight = textheight(s); // 计算文本的高度
		outtextxy(x - textWidth / 2, y - textHeight / 2 - 5, s);
	}
	else
	{
		float hudu = (2 - t / 12 * 2) * 3.14;
		line(x, y, x, y - r);  // 绘制指向12点位置的指针
		line(x, y, x + r * sin(hudu), y - r * cos(hudu));
		floodfill(x - 1, y - r + 1, RGB(181, 126, 230));
		setlinecolor(RGB(168, 80, 188));
		line(x, y, x, y - r * 2 / 3);
		line(x, y, x + r * sin(hudu) * 0.7, y - r * cos(hudu) * 0.7);
		swprintf_s(s, _T("%.1f"), t);
		int textWidth = textwidth(s); // 计算文本的宽度
		int textHeight = textheight(s); // 计算文本的高度
		outtextxy(x - textWidth / 2, y - textHeight / 2 - 5, s);
	}
}
void game_over()//游戏结束
{
	if (game_scene == 0)
	{
		int o = 0;//判断结局类型
		currentValue = -num_water_use + num_water_begin + num_water_boom;//避免鼠标多次点击偶然出现的水滴数1结局失败的bug
		if (num_water_boom == num_water_all)
			o = 1;
		else if (currentValue == 0 && water_over == 0)
			o = 2;
		else if (play_time <= 0&& num_water_boom!=0)
			o = 3;
			if (o != 0)
			{
				game_scene = 4;
				j = 0;
				// 定义并计算 "XX!" 文本
					TCHAR s1[40]; // 增加字符数组大小
					settextstyle(300, 0, _T("宋体")); // 设置文本样式
					if (o == 1) {
						settextcolor(GREEN);
						swprintf_s(s1, _T("AC!"));
					}
					else if (o == 2) {
						settextcolor(RED);
						swprintf_s(s1, _T("WA!"));
					}
					else if (o == 3)
					{
						settextcolor(RGB(181, 126, 230));
						swprintf_s(s1, _T("TLE!"));
					}
				int textWidth1 = textwidth(s1); // 计算文本的宽度
				int textHeight1 = textheight(s1); // 计算文本的高度
				outtextxy((width - textWidth1) / 2, height / 2 - textHeight1, s1);

				TCHAR s2[40]; // 增加字符数组大小
				settextstyle(100, 0, _T("宋体")); // 设置文本样式
				if (o == 1) {
					settextcolor(YELLOW);
					swprintf_s(s2, _T("再来一遍"));
				}
				else if (o == 2 || o == 3) {
					settextcolor(YELLOW);
					swprintf_s(s2, _T("不服再战"));
				}
				int textWidth2 = textwidth(s2); // 计算文本的宽度
				int textHeight2 = textheight(s2); // 计算文本的高度
				outtextxy((width - textWidth2) / 2 - 50, height / 3 * 2 - textHeight2, s2);

				TCHAR s3[40]; // 增加字符数组大小
				settextstyle(50, 0, _T("宋体")); // 设置文本样式
				if (o == 1) {
					settextcolor(YELLOW);
					swprintf_s(s3, _T("挑战其它"));
				}
				else if (o == 2 || o == 3) {
					settextcolor(YELLOW);
					swprintf_s(s3, _T("走此小道"));
				}
				int textWidth3 = textwidth(s3); // 计算文本的宽度
				int textHeight3 = textheight(s3); // 计算文本的高度
				// 确保 s3 的中心字符在 x * 3 / 4 处
				outtextxy(width - 200, 25, s3);
				Sleep(200);
				while (j == 0)
				{
					mouse();
					mouse_count();
					Sleep(1);
				}
			}
	}
}
